package net.siufung.market.base.permit.controller.regulation;

import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.market.base.permit.spi.IRoleUserCountSpi;

import java.util.Objects;

/**
 * @author 陈建峰
 * @since 2022/4/18 2:09 下午
 */
@AllArgsConstructor
public class RoleDeleteRegulation extends AbstractRegulation<Integer> {

    private static final long serialVersionUID = 4394853211638068929L;

    private final IRoleUserCountSpi roleUserCountSpi;

    @Override
    public boolean isSatisfiedBy(Integer roleId) {
        if(Objects.isNull(roleId)){
            throw new BizServiceException("角色主键为空！");
        }
        if(Objects.nonNull(roleUserCountSpi)){
            long existCount = roleUserCountSpi.getUserCountByRoleId(roleId);
            if(existCount > 0){
                throw new BizServiceException(String.format("当前角色被 %s 个用户使用，不能删除！", existCount));
            }
        }
        return Boolean.TRUE;
    }
}
